package top.buaarobot.webserver.web.rest;

import com.alibaba.fastjson.JSONObject;
import org.springframework.http.ResponseEntity;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import top.buaarobot.webserver.domain.VisitInfo;
import top.buaarobot.webserver.repository.VisitInfoRepository;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

@EnableScheduling
@RestController
@RequestMapping("/api/visit")
public class VisitInfoResource {

    private final VisitInfoRepository visitInfoRepository;
    private Date currentDate;

    public VisitInfoResource(VisitInfoRepository visitInfoRepository) {
        this.visitInfoRepository = visitInfoRepository;
        this.currentDate = new Date();
        // 若初始化时无当天的记录，做一次日常更新
        if(this.visitInfoRepository.findAllByDate(this.currentDate)==null){
            this.DailyUpdate();
        }
    }

    @PostMapping(value = "/visitOnce")
    public ResponseEntity<String> VisitOnce(){
        VisitInfo tmp = this.visitInfoRepository.findAllByDate(this.currentDate);
        tmp.setVisitTimes(tmp.getVisitTimes()+1);
        this.visitInfoRepository.save(tmp);
        return ResponseEntity.ok().build();
    }

    @Scheduled(cron = "0 0 0 * * *")  // 每日凌晨执行一次
    public void DailyUpdate() {
        this.currentDate = new Date();
        VisitInfo visitInfo = new VisitInfo();
        visitInfo.setVisitTimes(0);
        visitInfo.setDate(this.currentDate);
        visitInfoRepository.save(visitInfo);
    }

    /*@PostMapping(value = "/addInfo")
    public ResponseEntity<String> addVisitInfo(HttpServletRequest request, @RequestBody JSONObject json){
        VisitInfo visitInfo = new VisitInfo();
        visitInfo.setUserAgent(json.getString("userAgent"));
        visitInfo.setTimestamp(json.getString("timestamp"));
        visitInfo.setClientIp(getCliectIp(request));
        visitInfoRepository.save(visitInfo);
        return ResponseEntity.ok().build();
    }

    public static String getCliectIp(HttpServletRequest request)
    {
        String ip = request.getHeader("x-forwarded-for");
        if (ip == null || ip.trim() == "" || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.trim() == "" || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.trim() == "" || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }

        // 多个路由时，取第一个非unknown的ip
        final String[] arr = ip.split(",");
        for (final String str : arr) {
            if (!"unknown".equalsIgnoreCase(str)) {
                ip = str;
                break;
            }
        }
        return ip;
    }*/

}
